﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using ConnectDatabase;
using System.Data.SqlClient;
using System.Configuration;

namespace Crystal_Report_WithoutDB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        ConnectDB con = new ConnectDB("Data Source=Samurai-PC;Initial Catalog=CRM_SMS;Integrated Security=True");
        ReportDocument rptDoc = new ReportDocument();
        DataSet1 ds = new DataSet1();
        DataTable dt = new DataTable();

        private void button1_Click(object sender, EventArgs e)
        {
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                dt.TableName = "Crystal Report Example";
                dt = con.getReportYear("2012");
                ds.Tables[0].Merge(dt);
                rptDoc.Load(@"c:\Projects\Crystal_Report_WithoutDB\Crystal_Report_WithoutDB\CR_Thisyear.rpt");
                rptDoc.SetDataSource(ds);

                #region Cách truyền dữ liệu vào parameter
                #region khai báo biến
                ParameterFieldDefinitions crParameterFieldDefinitions;
                ParameterFieldDefinition crParameterFieldDefinition;
                ParameterValues crParameterValues = new ParameterValues();
                ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
                #endregion

                crParameterDiscreteValue.Value = "SMS REPORT IN 2012";
                crParameterFieldDefinitions = rptDoc.DataDefinition.ParameterFields;
                crParameterFieldDefinition = crParameterFieldDefinitions["title"];
                crParameterValues = crParameterFieldDefinition.CurrentValues;

                crParameterValues.Clear();
                crParameterValues.Add(crParameterDiscreteValue);
                crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
                #endregion

                crystalReportViewer1.ReportSource = rptDoc;
                crystalReportViewer1.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ExportPDF_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                ExportOptions CrExportOptions;
                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                SaveFileDialog saveDialog = new SaveFileDialog();

                saveDialog.InitialDirectory = "D:\\";
                saveDialog.Filter = "pdf files (*.pdf)|*.pdf";//|excel files (*.xls)|*.xls|All files (*.*)|*.*";
                saveDialog.FilterIndex = 1;
                //saveDialog.RestoreDirectory = true;
                saveDialog.OverwritePrompt = true;
                saveDialog.ShowHelp = false;
                saveDialog.Title = "Save report in pdf file";

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    CrDiskFileDestinationOptions.DiskFileName = saveDialog.FileName;
                    CrExportOptions = rptDoc.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    rptDoc.Export();
                    MessageBox.Show("Save complete", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void barLargeButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                ExportOptions CrExportOptions;

                DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                ExcelFormatOptions CrFormatTypeOptions = new ExcelFormatOptions();
                SaveFileDialog saveDialog = new SaveFileDialog();

                saveDialog.InitialDirectory = "D:\\";
                saveDialog.Filter = "excel files (*.xls)|*.xls";
                saveDialog.FilterIndex = 1;
                //saveDialog.RestoreDirectory = true;
                saveDialog.OverwritePrompt = true;
                saveDialog.ShowHelp = false;
                saveDialog.Title = "Save report in excel file";

                if (saveDialog.ShowDialog() == DialogResult.OK)
                {
                    CrDiskFileDestinationOptions.DiskFileName = saveDialog.FileName;
                    CrExportOptions = rptDoc.ExportOptions;
                    CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                    CrExportOptions.ExportFormatType = ExportFormatType.Excel;
                    CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                    CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    rptDoc.Export();
                    MessageBox.Show("Save complete", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.ServiceNotification);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}